package com.example.xianji.activity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.xianji.Contant;
import com.example.xianji.XJApplication;
import com.example.xianji.R;
import com.example.xianji.adapter.BaiKe_FenLei_Adapter;
import com.example.xianji.bean.BaiKe_Fenlei_DuiXiang;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class Main_BaiKe_FenLeiActivity extends Activity {
    /*************************百科分类列表***********************************/
    private ListView baike_parent_fenlei_listview, baike_child_fenlei_listview;
    private BaiKe_FenLei_Adapter mBaiKe_FenLei_Adapter, mChild_BaiKe_FenLei_Adapter;
    private ArrayList<BaiKe_Fenlei_DuiXiang> list = new ArrayList<BaiKe_Fenlei_DuiXiang>();
    private RelativeLayout baike_fenlei_back;
    private ArrayList<BaiKe_Fenlei_DuiXiang> cilid_list = new ArrayList<BaiKe_Fenlei_DuiXiang>();
    /********************listview 被点击的索引******************************/
    public static int parent_select_position = -1;
    private ArrayList<View> view_list = new ArrayList<View>();
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                baike_parent_fenlei_listview.setAdapter(mBaiKe_FenLei_Adapter);
            }
            if (msg.what == 2) {
                baike_child_fenlei_listview.setAdapter(mChild_BaiKe_FenLei_Adapter);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__bai_ke__fen_lei);

        baike_parent_fenlei_listview = (ListView) findViewById(R.id.baike_parent_fenlei_listview);
        baike_child_fenlei_listview = (ListView) findViewById(R.id.baike_child_fenlei_listview);
        baike_fenlei_back = (RelativeLayout) findViewById(R.id.baike_fenlei_back);
        mBaiKe_FenLei_Adapter = new BaiKe_FenLei_Adapter(this, list, parent_select_position);
        mChild_BaiKe_FenLei_Adapter = new BaiKe_FenLei_Adapter(this, cilid_list, parent_select_position);
        XJApplication.cachThreadPool.execute(new Runnable() {

            @Override
            public void run() {
                get_fenlei_data(0, 1);
            }
        });

        baike_parent_fenlei_listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                view_list.add(view);
                final int a = position;
                XJApplication.cachThreadPool.execute(new Runnable() {

                    @Override
                    public void run() {
                        get_fenlei_data(list.get(a).getId(), 2);
                    }
                });

                /************************下面这个方法使得点击某条目显示灰色，其余变白色**************************************/
                for (int i = 0; i < view_list.size(); i++) {
                    view_list.get(i).setBackgroundColor(Color.WHITE);
                }
                view.setBackgroundColor(Main_BaiKe_FenLeiActivity.this.getResources().getColor(R.color.grayF2));
                parent_select_position = position;
            }
        });

        baike_child_fenlei_listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                /***************作为选择分类的标记，选择后为1******************************/
                Main_BaiKeActivity.Choose_fenlei = 1;
                Main_BaiKeActivity.Cid = cilid_list.get(position).getId();
                mChild_BaiKe_FenLei_Adapter.notifyDataSetChanged();
                Main_BaiKe_FenLeiActivity.this.finish();
            }
        });

        baike_fenlei_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Main_BaiKe_FenLeiActivity.this.finish();
            }
        });
    }

    public void get_fenlei_data(int cid, final int number) {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        parms.addBodyParameter("cid", cid + "");
        httpUtils.send(HttpMethod.POST, Contant.baike_fenlei, parms, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                try {
                    JSONObject obj = new JSONObject(arg0.result);
                    if (obj.getInt("status") == 1) {
                        JSONArray array = obj.getJSONArray("data");
                        if (number == 2) {
                            /**************如果是二级就清理*********************/
                            cilid_list.clear();
                        }
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject item_object = array.getJSONObject(i);
                            BaiKe_Fenlei_DuiXiang mBaiKe_Fenlei_DuiXiang = new BaiKe_Fenlei_DuiXiang();
                            mBaiKe_Fenlei_DuiXiang.setId(item_object.getInt("id"));
                            mBaiKe_Fenlei_DuiXiang.setTitle(item_object.getString("title"));
                            mBaiKe_Fenlei_DuiXiang.setSpecial_parent(item_object.getInt("special_parent"));
                            if (number == 1) {
                                list.add(mBaiKe_Fenlei_DuiXiang);
                                handler.sendEmptyMessage(1);
                            } else if (number == 2) {
                                cilid_list.add(mBaiKe_Fenlei_DuiXiang);
                                handler.sendEmptyMessage(2);
                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }
}
